<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 系统用户 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="search-box">
                <el-form :inline="true">
                    <el-form-item label="用户名">
                        <el-input v-model="searchSet.name" prefix-icon="el-icon-search" clearable placeholder="请输入用户名"></el-input>
                    </el-form-item>
                    <el-form-item label="手机号">
                        <el-input v-model="searchSet.mobile" prefix-icon="el-icon-search" clearable placeholder="请输入手机号"></el-input>
                    </el-form-item>
                    <el-form-item label="用户状态">
                        <el-select v-model="searchSet.status" placeholder="选择状态" clearable>
                            <el-option v-for="(item, index) in statusList" :key="index" :label="item.name" :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                    <el-button type="primary" icon="el-icon-plus" @click="handleAddNew">新建用户</el-button>
                </el-form>
            </div>
            <el-table
                :data="tableData"
                v-loading="tableLoading"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
            >
                <el-table-column prop="name" label="用户名" align="center"></el-table-column>
                <el-table-column prop="mobile" label="手机号" align="center"></el-table-column>
                <el-table-column prop="addtime" label="注册时间" align="center"></el-table-column>
                <el-table-column label="角色权限" width="150" align="center">
                    <template slot-scope="scope">
                        <div>
                            <template v-for="(item, index) in scope.row.roles">
                                <el-tag :key="index" style="margin: 1px 3px" v-if="item.isChecked"> {{ item.rolename }}</el-tag>
                            </template>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="状态" width="90" align="center">
                    <template slot-scope="scope">
                        <el-tag :type="statusList[scope.row.status] ? statusList[scope.row.status].tagtype : 'info'">
                            {{ statusList[scope.row.status] ? statusList[scope.row.status].name : '无效' }}</el-tag
                        >
                    </template>
                </el-table-column>

                <el-table-column label="操作" width="340" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑信息与权限</el-button>
                        <el-button type="text" icon="el-icon-s-order" @click="handleDetail(scope.$index, scope.row)">详情</el-button>
                        <el-button
                            v-if="scope.row.status != 0"
                            type="text"
                            :class="scope.row.status == 1 ? 'color-red' : 'color-Green'"
                            icon="el-icon-warning"
                            @click="handleDisable(scope.row, scope.row.status == 1)"
                            >{{ scope.row.status == 1 ? '进行禁用' : '解除禁用' }}</el-button
                        >
                        <el-button type="text" class="color-Warning" icon="el-icon-key" @click="handleResetPassword(scope.row)"
                            >重置密码</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="sizes, total, prev, pager, next"
                    :current-page="query.index"
                    :page-sizes="[10, 20, 30, 40, 50, 100]"
                    :page-size="query.pageSize"
                    :total="itemTotal"
                    @current-change="handlePageChange"
                    @size-change="handlePageSizeChange"
                ></el-pagination>
            </div>
        </div>
        <el-dialog :title="editIsNewAdd ? '新建系统用户' : '编辑系统用户'" v-dialogDrag center :visible.sync="editVisible" width="60%">
            <el-form ref="form" :model="form" :rules="formRules" label-width="120px">
                <el-form-item label="用户名" prop="name">
                    <el-input
                        v-model="form.name"
                        placeholder="请填写用户名"
                        maxlength="15"
                        show-word-limit
                        clearable
                        class="formInput"
                    ></el-input>
                </el-form-item>
                <el-form-item label="手机号" prop="mobile">
                    <el-input
                        v-model="form.mobile"
                        placeholder="请填写手机号"
                        maxlength="20"
                        show-word-limit
                        clearable
                        class="formInput"
                    ></el-input>
                </el-form-item>
                <el-form-item label="角色权限" v-if="!editIsNewAdd">
                    <div class="FlexRowWarp">
                        <el-checkbox
                            size="mini"
                            :value="item.isChecked"
                            border
                            v-for="(item, index) in form.roles"
                            :key="index"
                            @change="
                                (e) => {
                                    item.isChecked = e;
                                }
                            "
                            >{{ item.rolename }}</el-checkbox
                        >
                    </div>
                </el-form-item>
            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
        <detailDialog :visible.sync="detailVisible" :data="detailInfo" :dataType="detailType"></detailDialog>
    </div>
</template>

<script>
import API from '../../../api/index'; //读入restfulAPI
import StringTool from '../../../utils/StringTool';
import JSTool from '../../../utils/JSTool';
import dataTable from './dataTable';
import moment from 'moment';
import detailDialog from '../../common/dialog/detailDialog.vue';
export default {
    name: 'sysuserTable',
    mixins: [dataTable],
    components: {
        detailDialog
    },
    data() {
        return {
            searchSet: {
                name: '',
                mobile: '',
                status: ''
            },
            query: {
                index: 1,
                pageSize: 10
            },
            statusList: {
                0: { name: '无效', id: '0', tagtype: 'info' },
                1: { name: '正常', id: '1', tagtype: 'success' },
                2: { name: '已禁用', id: '2', tagtype: 'danger' }
            },

            editVisible: false,
            editIsNewAdd: false,
            form: {},
            formRules: {
                mobile: [
                    { required: true, message: '参数不能为空', trigger: 'change' },
                    { min: 8, max: 20, message: '长度在 8 到 15 个字符', trigger: 'change' }
                ],
                name: [
                    { required: true, message: '参数不能为空', trigger: 'change' },
                    { min: 2, max: 15, message: '长度在 2 到 20 个字符', trigger: 'change' }
                ]
            },
            detailVisible: false,
            detailInfo: {},
            detailType: [
                {
                    attrs: { column: 2 },
                    list: [
                        { label: 'ID', key: 'id' },
                        { label: '名称', key: 'name' },
                        { label: '手机号码', key: 'mobile' },
                        { label: '添加时间', key: 'addtime' }
                    ]
                }
            ]
        };
    },
    created() {
        this.init();
    },
    mounted() {
        // this.PreviewPhotoList = ["http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"]
        // this.$refs.PreviewImage.clickHandler()
    },
    methods: {
        //页面初始化
        init() {
            this.getData();
            // this.$refs.PreviewImage.clickHandler()
        },

        getData() {
            this.tableLoading = true;
            API.StandardDoWith(API.Sysuser.Get(this.getQuery()), this, {
                success: (detail) => {
                    this.itemTotal = detail.count;
                    this.tableData = detail.list;
                },
                complete: () => {
                    this.tableLoading = false;
                }
            });
        },
        handleAddNew() {
            this.editVisible = true;
            this.editIsNewAdd = true;
            this.form = {
                mobile: '',
                name: ''
            };
        },
        handleEdit(index, row) {
            this.form = JSTool.DeepClone(row);
            this.editIsNewAdd = false;
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    if (this.editIsNewAdd) {
                        this.requestAddNew(this.form);
                    } else {
                        this.requestModify(this.form);
                    }
                } else {
                    this.$message.error(`表单验证失败，无法提交`);
                    return false;
                }
            });
        },
        requestAddNew(data) {
            API.StandardDoWith(API.Sysuser.Post(data), this, {
                loading: '新建用户处理中',
                success: (detail) => {
                    this.editVisible = false;
                    this.$message.success(`新建用户成功`);
                    this.getData();
                }
            });
        },
        requestModify(data) {
            API.StandardDoWith(API.Sysuser.Put(data), this, {
                loading: '编辑用户处理中',
                success: (detail) => {
                    this.editVisible = false;
                    this.$message.success(`编辑用户成功`);
                    this.getData();
                }
            });
        },
        handleDetail(index, row) {
            let obj = Object.assign({}, row);
            this.detailInfo = obj;
            this.detailVisible = true;
        },

        handleResetPassword(item) {
            this.$confirm('确定要重置密码吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    API.StandardDoWith(API.Sysuser.Patch({ params: { id: item.id } }), this, {
                        loading: '重置密码处理中',
                        success: (detail) => {
                            this.$message.success(`重置密码成功`);
                        }
                    });
                })
                .catch(() => {});
        },
        handleDisable(row, disable) {
            let tip = disable ? '进行禁用' : '解除禁用';
            this.$confirm(`确定要${tip}吗？`, '提示', {
                type: 'warning'
            })
                .then(() => {
                    API.StandardDoWith(API.SysUserExtra.Put({ id: row.id, disable: disable }), this, {
                        loading: tip + '处理中',
                        success: (detail) => {
                            this.$message.success(`${tip}成功`);
                            this.getData();
                        }
                    });
                })
                .catch(() => {});
        }
    }
};
</script>

<style scoped>
@import '../../../assets/css/dataTable.css';
.el-checkbox {
    margin-right: 0;
}
</style>
